#![feature(plugin)]
#![plugin(rocket_codegen)]
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate serde;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate rocket;
extern crate rocket_contrib;
extern crate reqwest;

mod http;
mod config;
mod undercover;

use http::CqpActionExt;

fn main() {
    env_logger::init().unwrap();

    let config = config::Config::from_file("config.json");
    config.save_to_file("config.json");

    let mut holder = http::CqpEvent::new();
    let action = http::CqpAction::new(config.post_path.clone());

    if let Some((user_id, user_name)) = action.get_login_info() {
        info!("{} ({}) 已登入", user_name, user_id);
    } else {
        error!("接入失败！");
        return;
    }

    holder.insert_handler(Box::new(undercover::Undercover{}));

    http::loop_web(holder, action);
}